﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponentsDx.Chat;
using ProgNetComponentsDx.Options;
using System.Threading.Tasks;

namespace BuildHelper.Kontakt
{
    public class ServerNew : ChatServer
    {
        static ServerNew instance = null;
        public static ServerNew Instance
        {
            get { return instance; }
        }

        static ServerNew()
        {
            OptionCache.OptionChanged += new OptionChangedEventHandler(OptionCache_OptionChanged);
        }
        public static void StartServer()
        {
            if (instance == null)
            {
                int port = OptionCache.GetOptionValue(Init.InitFormOptionNames.Kontakty_Group.Server.Port).AsInt();
                string host = OptionCache.GetOptionStringValue(Init.InitFormOptionNames.Kontakty_Group.Server.HostIp);
                instance = new ServerNew(port);
                instance.Start();
                instance.MyClient = new ClientNew(host,port);
                instance.MyClient.StartOrWait();
            }
        }
        public static void StopServer()
        {
            if (instance != null)
            {
                if (instance.MyClient != null)
                {
                    instance.MyClient.Stop();
                    instance.MyClient.Dispose();
                    instance.MyClient = null;
                }
                instance.Stop();
                instance = null;
            }
        }
        public static void RestartServer()
        {
            if (instance != null)
            {
                StopServer();
                StartServer();
            }
        }

        static void OptionCache_OptionChanged(object optionId)
        {
            if (Init.InitFormOptionNames.Kontakty_Group.Server.Port.IsTheSameKey(optionId))
            {
                RestartServer();
            }
        }
        
        public ClientNew MyClient
        {
            get;
            set;
        }

        public ServerNew(int port)
            : base(port)
        {
            
        }

        internal void SendToAll(string type, string msg)
        {
            ChatMessageInfo info = new ChatMessageInfo();
            info.Message = msg;
            info.Type = type;
            info.User = KontaktTools.ServerName;
            SendMessageToAll(info);
        }

        internal void SendTo(string type, string user, string msg)
        {
            ChatMessageInfo info = new ChatMessageInfo();
            info.Message = msg;
            info.Type = type;
            info.User = KontaktTools.ServerName;
            SendMessage(user, info);
        }

        protected override void OnMessageReceived(string from, ChatMessageInfo info)
        {
            base.OnMessageReceived(from, info);
        }

        protected override bool ShouldSendBeep()
        {
            if (KontaktUsersForm.Instance != null)
                return true;
            if (MyClient != null && MyClient.IsFormVisible())
                return true;
            return false;
        }
    }
}
